<?php
    $cropID=$_GET["cropID"];
    $cropClass=new crop($cropID);
    $crop=$cropClass->getCrop();
    $farmID=$crop["farmID"];
    
    $farmClass=new farm();
    $farmClass->setFarmID($farmID);
    $farm=$farmClass->getFarm();
    $memID=$farm["memID"];
    
    $memberClass=new member($memID);
    $member=$memberClass->getMember();
    
    $dateClass=new date();
    ?>
<link href="member/member.css" rel="stylesheet" type="text/css"/>
<style type="text/css">
    @media (orientation: landscape){
        #cropCostInputManageDIV{
            width: 60%;
            height: 100%;
            float: left;
        }
    }
    @media (orientation: portrait){
        #cropCostInputManageDIV{
            width: 100%;
            height: 70%;
            margin-bottom: 5px;
        }
    }
    #cropCostInputManageDIV{
        padding: 5px;
        overflow: auto;
    }
</style>
<?php include "member/memberDetailDIV.php";?>
<div id="cropCostInputManageDIV">
    <div class="whiteShadow" style="overflow: auto;width: 100%;height: 100%;padding: 5px;">
        <table style="width: 100%;">
            <tr>
                <td style="text-align: center;">
                    <input type="button" class="btn btn-default" value="< เลือก Crop" style="float: left;"
                        onclick="window.open('?page=member/crop/cropList&farmID=<?=$farmID?>','_self')"
                        >
                    <?php
                        $dateClass=new date();
                        echo $dateClass->dateEncodeBE($crop["startDate"]);
                        echo "-";
                        echo $dateClass->dateEncodeBE($crop["endDate"]);
                        ?>
                    <input type="button" class="btn btn-default" value="บันทึกรายรับ >" style="float: right;"
                        onclick="window.open('?page=member/crop/cropIncomeInput&cropID=<?=$_GET["cropID"]?>','_self')"
                        >
                </td>
            </tr>
            <tr>
                <td style="padding-top: 5px;padding-left: 10px;padding-right: 10px;">
                    <center>แบบฟอร์มบันทึก <b>ต้นทุน</b></center><br>
                    <form id="addCostForm" action="member/crop/cropCostSave.php" method="post" class="form-horizontal" target="tempFrame">
                        <input type="hidden" name="cropID" value="<?=$cropID?>">
                        <fieldset>
                            <?php
                                $queryCT="
                                    select  *
                                    from    cost_list
                                    where   parent is null
                                    ";
                                $resultCT=$mysqli->query($queryCT);
                                $iCT=1;
                                while($rowCT=$resultCT->fetch_array(MYSQLI_ASSOC)){
                                    ?>
                            <input type="hidden" name="costID[]" value="<?=$rowCT["costID"]?>">
                            <!-- Appended Input-->
                            <div class="form-group">
                                <label class="col-md-4 control-label" for="appendedtext"><?=$rowCT["costName"]?></label>
                                <div class="col-md-4">
                                    <div class="input-group">
                                        <input id="cost<?=$rowCT[costID]?>" name="cost[]" class="form-control" placeholder="<?=$rowCT["costName"]?>" type="tel" 
                                            style="text-align: right;min-width: 100px;"
                                            value="<?=$cropClass->getCost($rowCT["costID"])?>"
                                            onkeyup="
                                                var total = $(this).val()*<?=$farm["area"]?>;
                                                $('#total<?=$rowCT[costID]?>').html(number_format(total));
                                            "
                                            >
                                        <span class="input-group-addon">บาท/ไร่</span>
                                        <span class="input-group-addon" style="min-width: 100px;text-align: right;">
                                            <span id="total<?=$rowCT["costID"]?>"></span>
                                            บาท
                                        </span>
                                    </div>
                                    <script type="text/javascript">
                                        $('#total<?=$rowCT[costID]?>').html(number_format($('#cost<?=$rowCT["costID"]?>').val()*<?=$farm["area"]?>));
                                    </script>
                                </div>
                            </div>
                                        <?php
                                    $iCT++;
                                }
                            ?>
                        </fieldset>
                    </form>
                </td>
            </tr>
        </table>
    </div>
</div>
<iframe name="tempFrame" style="display: none;"></iframe>
<script type="text/javascript">
    $('input').change(function(){
        $.post('member/crop/cropCostSave.php',
            $('#addCostForm').serialize()
        ,function(data){
            
        });
    });
</script>